#!/usr/bin/python

# User 2%, System 4%, IOW 0%, IRQ 0%
# User 13 + Nice 0 + Sys 31 + Idle 579 + IOW 0 + IRQ 0 + SIRQ 0 = 623
import string
from Myconfig import *

class CpuMonitor:

        ########################################################
        
	def __init__(self):
            self.name = 'CpuMonitor'
            #  old new 
            self.User = [0,0]
	    self.Nice = [0,0]
	    self.Sys = [0,0]
	    self.Idle = [0,0]
	    self.IOW = [0,0]
	    self.IRQ = [0,0]
	    self.SIRQ = [0,0]
            self.Total = [0,0]
            self.namelist = [['cpu',0xff00ff], \
                     ['cpu0',0x112200], \
                     ['cpu1',0xff7f58],  \
                     ['cpu2',0xff0000], \
                     ['cpu3',0x44ff55], \
                     ]

            self.monitorcontainer={}
            self.cpudata_old = {'cpu':[0,0,0,0,0,0,0,0], \
                          'cpu0':[0,0,0,0,0,0,0,0], \
                          'cpu1':[0,0,0,0,0,0,0,0], \
                          'cpu2':[0,0,0,0,0,0,0,0], \
                          'cpu3':[0,0,0,0,0,0,0,0], \
                          }
            self.cpudata_new = {'cpu':[0,0,0,0,0,0,0,0], \
                          'cpu0':[0,0,0,0,0,0,0,0], \
                          'cpu1':[0,0,0,0,0,0,0,0], \
                          'cpu2':[0,0,0,0,0,0,0,0], \
                          'cpu3':[0,0,0,0,0,0,0,0], \
                          }

        def  getCPUrate(self,name):
             temp = float(int(self.cpudata_new[name][3])-int(self.cpudata_old[name][3]))/   \
                     float(int(self.cpudata_new[name][7])-int(self.cpudata_old[name][7]))
             temp = 1 - temp
             #print temp
             return temp*100

        def  parse(self,s): 
            s=s.strip()
            sitem=string.split(s)
            if 'cpu' not in s:
                return -1;
            # two cores 
            #cpu  11937 408 12814 141702 3902 2 113 0 0 0
            #cpu0 7020 219 8514 63880 3260 2 101 0 0 0
            #cpu1 4917 189 4300 77822 642 0 12 0 0 0

            
            #  four cores 
            #cpu  3793945 104696 1276563 46787875 345859 184 10397 0 0 0
            #cpu0 2777435 51830 976299 8594431 274469 69 9155 0 0 0
            #cpu1 789109 40419 207105 12087689 56964 53 968 0 0 0
            #cpu2 162857 9774 62312 12978118 10256 33 224 0 0 0
            #cpu3 64544 2673 30847 13127637 4170 29 50 0 0 0

            """
            if sitem[0] == 'cpu':
                myindex = 0
            else:
                myindex = int(item[0][len("cpu"):]) +1
            print 'myindex is %d'%(myindex) 
            """
            name = sitem[0]

            for index in range(0,8):
                self.cpudata_old[name][index] = self.cpudata_new[name][index]

            mysum = 0
            for index in range(0,7):
                self.cpudata_new[name][index] = sitem[index+1]
                #print 'index is %d'%(index)
                mysum = mysum + int(sitem[index+1])
            self.cpudata_new[name][7] = mysum

            #print self.cpudata_old
            #print self.cpudata_new

            """
            self.Total[0] = (float(self.User[0])+float(self.Nice[0])+float(self.Sys[0]) \
                       +float(self.IOW[0])+float(self.IRQ[0])+float(self.SIRQ[0]))+float(self.Idle[0]);
            self.User[1] = item[1]
	    self.Nice[1] = item[2]
	    self.Sys[1] = item[3]
	    self.Idle[1] = item[4]
	    self.IOW[1] = item[5]
	    self.IRQ[1] = item[6]
	    self.SIRQ[1] = item[7]
            self.Total[1] = (float(self.User[1])+float(self.Nice[1])+float(self.Sys[1]) \
                       +float(self.IOW[1])+float(self.IRQ[1])+float(self.SIRQ[1]))+float(self.Idle[1]);
            """
            if  self.cpudata_old[name][7] == 0:
                print ' no enough data'
                return -2;
            
            value=self.getCPUrate(name)/2
            print name + 'rate is %f'%(value)

            #print  'memory'
            for cpuitem in self.namelist:
                if name != cpuitem[0]:
                    print 'item is :'+sitem[0]+'  cpuitem is '+ cpuitem[0]
                    continue
                temp =[]
                if self.monitorcontainer.has_key(name) :
                    self.monitorcontainer[name].append(value)
                    if len(self.monitorcontainer[name]) > Myconfig.samplecount:
                        del self.monitorcontainer[name][0]    
                else:
                    temp.append(value)
                    self.monitorcontainer[name]=temp

            
                        
 





